/*
 * Copyright (C) 2011 TargetBoard
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package jaxb;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

/**
 * @author amc
 *
 */
public class XmlInjector<T> {
	private static final String JAXB_CLASSES = "jaxb.generated.datatable:jaxb.generated.users:jaxb.generated.book";
	private String path;
	public XmlInjector(String path) {
		this.path = path;
	}
	private Marshaller getMarshaller() throws JAXBException{
		JAXBContext jc = JAXBContext.newInstance(JAXB_CLASSES);
		return jc.createMarshaller();
	}
	public void inject(JAXBElement<T> data) {
		try {
			Marshaller m = getMarshaller();
			m.marshal(data,new FileOutputStream(new File(path)));
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
